<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/30
 * Time: 11:54
 */

namespace app\common\controller;


use app\api\controller\Pub;
use app\common\model\CardConsume;
use app\common\model\CardCzgive;
use app\common\model\CardRank;
use app\common\model\CardUserGet;
use Wechat\Wxobjopen;

class CardDedu extends Wxoption
{
    //获取折扣
    public function getzk($arr, $user, $cardus)
    {
      $Pub=new Pub();
        //判断价格是否等于0.01
        if ($arr['add_balance']!=="0.01") {
                //判断是否折扣开关

            if ($cardus['is_cz_discount'] == 1) {
                $discount = $cardus['discount'] / 100;                  //折扣比率
                $arrs['del_balance'] = $arr['add_balance'] * $discount;     //需回传的支付金额
                $arrs['dis_card_zk'] = $Pub->format_zkprice(bcsub($arr['add_balance'] , $arrs['del_balance'],2));     //会员卡折扣金额
            } else {
                $discount = 1;
                $arrs['del_balance'] = $arr['add_balance'] * $discount;     //需回传的支付金额
                $arrs['dis_card_zk'] = bcsub($arr['add_balance'] , $arrs['del_balance'],2);     //会员卡折扣金额
            }
            $dis_h = $arrs['del_balance'];          //折扣后金额
            //判断等级折扣开关
            if ($cardus['is_cz_dj_discount'] == 1) {
                $card_id = $user['card_id'];
                $level = getlive($card_id, $user['bonus']);
                $CardRank = new CardRank();
                $rankinfo = $CardRank->where(['card_id' => $card_id, 'live' => $level])->find();
                //判断该等级是否折扣
                if ($rankinfo['discount']) {
                    $rankdis = $rankinfo['discount'] / 100;
                    $arrs['del_balance'] = $arrs['del_balance'] * $rankdis;     //需回传的支付金额
                    $arrs['vip_card_zk'] = $Pub->format_zkprice(bcsub($dis_h , $arrs['del_balance'],2));       //等级折扣金额
                } else {
                    $arrs['vip_card_zk'] = 0;       //等级折扣金额
                }
            } else {
                $arrs['vip_card_zk'] = 0;       //等级折扣金额
            }
            //判断卡券抵扣
            if ($cardus['is_dedu_cards'] == 1) {
                $cdata['price'] = $arrs['del_balance'];                         //传向抵扣方法的支付金额
                $dedu_res = $this->card_deducted($user, $cdata);       //抵扣返回
                $dedu_num = $arrs['del_balance'] - $dedu_res['gel_card_zk'];                    //实际支付
                //END
                $arrs['del_balance'] = $Pub->format_zkprice($dedu_num);    //返回抵扣结果
                $arrs['hx_card'] = $dedu_res['hx_card'];                    //抵扣的卡券
                $arrs['gel_card_zk'] = $dedu_res['gel_card_zk'];            //卡券抵扣的金额

            } else {
                $arrs['hx_card'] = '0000';                          //抵扣的卡券
                $arrs['gel_card_zk'] = 0;                           //卡券抵扣的金额
            }
        } else {
            $arrs['dis_card_zk'] = 0;                                                 //会员折扣
            $arrs['vip_card_zk'] = 0;                                                     //等级折扣
            $arrs['gel_card_zk'] = 0;
            $arrs['del_balance']=$arr['add_balance'];
            $arrs['hx_card'] ="";
            $arrs['gel_card_zk'] = "";
        }
        return $arrs;
    }


    public function paygetzk($arr, $user, $cardus)
    {
        $Pub=new Pub();
        //判断价格是否等于0.01
        if ($arr['add_balance']!=="0.01") {
            //判断是否折扣开关

            if ($cardus['is_zf_discount'] == 1) {
                $discount = $cardus['discount'] / 100;                  //折扣比率
                $arrs['del_balance'] = $arr['add_balance'] * $discount;     //需回传的支付金额
                $arrs['dis_card_zk'] = $Pub->format_zkprice(bcsub($arr['add_balance'] , $arrs['del_balance'],2));     //会员卡折扣金额
            } else {
                $discount = 1;
                $arrs['del_balance'] = $arr['add_balance'] * $discount;     //需回传的支付金额
                $arrs['dis_card_zk'] = bcsub($arr['add_balance'] , $arrs['del_balance'],2);     //会员卡折扣金额
            }
            $dis_h = $arrs['del_balance'];          //折扣后金额
            //判断等级折扣开关
            if ($cardus['is_dj_discount'] == 1) {
                $card_id = $user['card_id'];
                $level = getlive($card_id, $user['bonus']);
                $CardRank = new CardRank();
                $rankinfo = $CardRank->where(['card_id' => $card_id, 'live' => $level])->find();
                //判断该等级是否折扣
                if ($rankinfo['discount']) {
                    $rankdis = $rankinfo['discount'] / 100;
                    $arrs['del_balance'] = $arrs['del_balance'] * $rankdis;     //需回传的支付金额
                    $arrs['vip_card_zk'] = $Pub->format_zkprice(bcsub($dis_h , $arrs['del_balance'],2));       //等级折扣金额
                } else {
                    $arrs['vip_card_zk'] = 0;       //等级折扣金额
                }
            } else {
                $arrs['vip_card_zk'] = 0;       //等级折扣金额
            }
            //判断卡券抵扣
            if ($cardus['is_dedu_cards'] == 1) {
                $cdata['price'] = $arrs['del_balance'];                         //传向抵扣方法的支付金额
                $dedu_res = $this->card_deducted($user, $cdata);       //抵扣返回
                $dedu_num = $arrs['del_balance'] - $dedu_res['gel_card_zk'];                    //实际支付
                //END
                $arrs['del_balance'] = $Pub->format_zkprice($dedu_num);    //返回抵扣结果
                $arrs['hx_card'] = $dedu_res['hx_card'];                    //抵扣的卡券
                $arrs['gel_card_zk'] = $dedu_res['gel_card_zk'];            //卡券抵扣的金额

            } else {
                $arrs['hx_card'] = '0000';                          //抵扣的卡券
                $arrs['gel_card_zk'] = 0;                           //卡券抵扣的金额
            }
        } else {
            $arrs['dis_card_zk'] = 0;                                                 //会员折扣
            $arrs['vip_card_zk'] = 0;                                                     //等级折扣
            $arrs['gel_card_zk'] = 0;
            $arrs['del_balance']=$arr['add_balance'];
            $arrs['hx_card'] ="";
            $arrs['gel_card_zk'] = "";
        }
        return $arrs;
    }

    //卡券抵扣
    public function card_deducted($user, $data)
    {
        $Pub=new Pub();
        $map['openid'] = $user['openid'];
        $map['statu'] = ['neq', 2];
        $map['card_type'] = ['in', ['CASH', 'DISCOUNT']];
        $map['have'] = 1;
        $map['end_time'] = ['gt',time()];
        $CardUserGet = new CardUserGet();
        $cardli = $CardUserGet->where($map)->field('card_id,code,card_type')->select();

        if ($cardli) {
            $should = $data['price'];           //应付
            foreach ($cardli as $k => $v) {
                if (filteroffcard($v['card_id']) == true && filtercarddedu($v['card_id']) == true) {
                    $cardlist[$k] = $v;
                } else {
                    $cardlist = [];
                }
            }

            if (!empty($cardlist)) {
                //使用最先领取的一张
                foreach ($cardlist as $k => $v) {
                    //折扣券
                    if ($v['card_type'] == 'DISCOUNT') {
                        if (isset($card_id)) {
                            if (filtercarddedupile($v['card_id']) == false || $v['card_id'] == $card_id) {      //在已产生抵扣时判断是否支持叠加 / 本次循环card_id是否和上一次循环card_id相同
                                continue;
                            }
                        }
                        $discount = getcardgeldis($v['card_id']) / 100;
                        $data['price'] *= $discount;
                        $data1['hx_card'][$k]['card_id'] = $v['card_id'];
                        $data1['hx_card'][$k]['code'] = $v['code'];
                        if (filtercarddedupile($v['card_id']) == false) {
                            break;
                        } else {
                            $card_id = $v['card_id'];
                        }
                    } elseif ($v['card_type'] == 'CASH') {
                        //代金券
                        $cost = getcardgelcash($v['card_id']);              //获取代金券抵扣
                        if ($data['price'] >= $cost['least_cost']) {
                            if ($data['price'] - $cost['reduce_cost'] > 0.01) {     //抵用后金额是否可支付
                                if (isset($card_id)) {
                                    if (filtercarddedupile($card_id) == false || $v['card_id'] == $card_id) {        //在已产生抵扣时判断是否支持叠加 / 本次循环card_id是否和上一次循环card_id相同
                                        continue;
                                    }
                                }
                                $data['price'] = $data['price']-$cost['reduce_cost'];
                                $data1['hx_card'][$k]['card_id'] = $v['card_id'];
                                $data1['hx_card'][$k]['code'] = $v['code'];
                                if (filtercarddedupile($v['card_id']) == false) {        //判断是否可叠加
                                    break;
                                } else {
                                    $card_id = $v['card_id'];
                                }
                            }
                        }
                    }
                }

                //排列要核销的卡券
                if (isset($data1['hx_card'])) {
                    $gel_card_zk=$should - $data['price'];

                    $data['gel_card_zk'] = $Pub->format_zkprice($gel_card_zk);
                    if($data['gel_card_zk']!==0){
                        $data1['hx_card'] = array_values($data1['hx_card']);
                        $data['hx_card'] = json_encode($data1['hx_card'], JSON_UNESCAPED_UNICODE);
                    }else{
                        $data['hx_card'] ='0000';
                    }

                    //返回数据

                } else {
                    //返回数据
                    $data['hx_card'] = '0000';
                    $data['gel_card_zk'] = 0;
                }
//                return $data;
            } else {
                $data['hx_card'] = '0000';
                $data['gel_card_zk'] = 0;
            }
        } else {
            $data['hx_card'] = '0000';
            $data['gel_card_zk'] = 0;
        }
        return $data;
    }


    /*
    * 核销抵扣卡券
    *
    *
    * $data orderid  核销记录参数
    * */
    public function pay_hx_card($param)
    {
        $CardUserGet = new CardUserGet();
        $CardConsume = new CardConsume();
        $Paylog = new \app\common\model\Paylog();
        $payinfo = $Paylog->where(['orderid' => $param['orderid']])->field('hx_card,mid,openid,d_cashier,gel_card_zk')->find();
        if ($payinfo['gel_card_zk'] > 0) {
            $hx_card = json_decode($payinfo['hx_card'], true);
            if (isset($hx_card) && is_array($hx_card)) {
                $options = $this->options($payinfo['mid'], 'mid');
                $wxObj = new Wxobjopen($options);
                foreach ($hx_card as $k => $v) {
                    $res[$k] = $wxObj->consumeCode($v);
                    if (isset($res[$k]['errcode']) && $res[$k]['errcode'] == 0) {
                        $CardUserGet->where(['code'=>$v['code'],'card_id'=>$v['card_id']])->update(['statu' => 2]);
                        //核销记录
                        $conlog[$k]['card_id'] = $v['card_id'];
                        $conlog[$k]['code'] = $v['code'];
                        $conlog[$k]['card_type'] = get_cardid_type_lden($v['card_id']);
                        $conlog[$k]['openid'] = $payinfo['openid'];
                        $conlog[$k]['mid'] = $payinfo['mid'];
                        $conlog[$k]['hx_fs'] = '消费抵扣';
                        $conlog[$k]['con_username'] = $payinfo['d_cashier'];
                        $conlog[$k]['create_time'] = time();
                        $resu = '0000';
                    } else {
                        $resu = '9009';
                        $CardUserGet->where(['code'=>$v['code'],'card_id'=>$v['card_id']])->update(['statu' => 2]);
                    }

                }
                if (isset($res)) {
                    $resut = $res;
                } else {
                    $resut = '';
                }
                if (isset($conlog)) {
                    $CardConsume->allowField(true)->saveAll($conlog);
                }
                $pdata['hx_card_res'] = json_encode($resut, JSON_UNESCAPED_UNICODE);
                $pres = $Paylog->allowField(true)->save($pdata, ['orderid' => $param['orderid']]);
                if ($pres) {
                    $resu = 0;
                } else {
                    $resu = 9008;
                }
            } else {
                $resu = 9009;
            }
        } else {
            $resu = 0000;
        }
        return $resu;
    }

}